package cn.edu.hust.youth.tzb.service;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSession;

public class OnlineUserCounterListener implements HttpSessionAttributeListener {
    public void attributeAdded(HttpSessionBindingEvent event) {
        String Attributr_name = (String)event.getName();
        HttpSession session = (HttpSession)event.getSession();
        //System.out.println("Attributr_name:"+Attributr_name);
        if (Attributr_name.equals("STUDENT")) {
            OnlineCounter.raiseStudent();
            OnlineList.student_list.add(event.getValue());
            //System.out.println("Student:"+OnlineCounter.getOnlineStudent());
        } else if (Attributr_name.equals("MANAGER")) {
            OnlineCounter.raiseManager();
            OnlineList.manager_list.add(event.getValue());
            //System.out.println("Manager:"+OnlineCounter.getOnlineManager());
        }
    }

    public void attributeReplaced (HttpSessionBindingEvent event) {

    }

    public void attributeRemoved (HttpSessionBindingEvent event) {
        String Attributr_name = (String)event.getName();
        if (Attributr_name.equals("STUDENT")) {
            OnlineCounter.reduceStudent();
            OnlineList.student_list.remove(event.getValue());
        } else if (Attributr_name.equals("MANAGER")) {
            OnlineCounter.reduceManager();
            OnlineList.manager_list.remove(event.getValue());
        }
    }
}
